<?php 
class Tree { 
    public $arr = array();
    public $icon = array('│','├','└');
    public $nbsp = "&nbsp;";
    public $ret = ''; 
    

    public function __construct($arr=array()) {
        $this->arr = $arr;
        $this->ret = '';
        return is_array($arr);
    }
    /**
     * 将数据格式化成树形结构 
     * @param array $items
     * @return array 
     */ 
    function getTree($items,$id='id',$pid='parent_id',$son = 'children'){
      $tree = array(); //格式化的树
      $tmpMap = array(); //临时扁平数据
        
      foreach ($items as $item) {
        $tmpMap[$item[$id]] = $item;
      }
        
      foreach ($items as $item) {
        if (isset($tmpMap[$item[$pid]])) {
          $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
        } else {
          $tree[] = &$tmpMap[$item[$id]];
        }
      }
      unset($tmpMap);
      return $tree;
    }

    public function getChild($bid){
        $a = $newarr = array();
        if(is_array($this->arr)){
            foreach($this->arr as $id => $a){
                if($a['parent_id'] == $bid) $newarr[$id] = $a;
            }
        }
        return $newarr ? $newarr : false;
    }
    function getSelectTree($bid, $str, $sid = 0, $adds = '', $strgroup = ''){
        $number=1;
        $child = $this->getChild($bid);
        if(is_array($child)){
            $total = count($child);
            foreach($child as $id=>$a){
                $j=$k='';
                if($number==$total){
                    $j .= $this->icon[2];
                }else{
                    $j .= $this->icon[1];
                    $k = $adds ? $this->icon[0] : '';
                }
                $spacer = $adds ? $adds.$j : '';
                if(empty($a['selected'])){
                    $selected = ($a['id']==$sid) ? 'selected' : '';
                }
                @extract($a); 
                $parent_id == 0 && $strgroup ? eval("\$newstr = \"$strgroup\";") : eval("\$newstr = \"$str\";");
                $this->ret .= $newstr;
                $nbsp = $this->nbsp;
                $this->getSelectTree($id, $str, $sid, $adds.$k.$nbsp,$strgroup);
                $number++;
            }
        }
        return $this->ret;
    } 

    //递归获取所有的子分类的ID
    function getAllChild($categorys,$catId){
         $subs=array();   
        foreach($categorys as $item){  
            if($item['parent_id']==$catId){   
                $subs[]=$item['id'];  
                $subs=array_merge($subs,$this->getAllChild($categorys,$item['id']));   
            }  
                  
        }  
        return $subs;  
    }
    //获取某个分类的所有父分类   
    function getAllParents($categorys,$catId){   
        $tree=array();  
        foreach($categorys as $item){  
            if($item['id']==$catId){  
                if($item['parent_id']>0)  { 
                    $tree=array_merge($tree,$this->getAllParents($categorys,$item['parent_id'])); 
                }    
                $tree[]=$item['id']; 
                break;    
            }  
        }  
        return $tree;  
    }  

    function getTrTree($bid, $str, $sid = 0, $adds = '', $strgroup = ''){
        $number=1;
        $child = $this->getChild($bid);
        if(is_array($child)){
            $total = count($child);
            foreach($child as $id=>$a){
                $j=$k='';
                if($number==$total){
                    $j .= $this->icon[2];
                }else{
                    $j .= $this->icon[1];
                    $k = $adds ? $this->icon[0] : '';
                }
                $spacer = $adds ? $adds.$j : '';
                if(empty($a['selected'])){
                    $selected = ($a['id']==$sid) ? 'selected' : '';
                }
                @extract($a); 
                $parent_id == 0 && $strgroup ? eval("\$newstr = \"$strgroup\";") : eval("\$newstr = \"$str\";");
                $this->ret .= $newstr;
                $nbsp = $this->nbsp;
                $this->getSelectTree($id, $str, $sid, $adds.$k.$nbsp,$strgroup);
                $number++;
            }
        }
        return $this->ret;
    }
}
?>